package com.example.demo.service;

import com.example.demo.dao.DrugsMapper;
import com.example.demo.dao.PurchaseMapper;
import com.example.demo.entity.CommonResult;
import com.example.demo.entity.MInpurchase;
import com.example.demo.entity.drugs;
import com.example.demo.entity.purchasegoods;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class PurchaseService implements Purchaseimpl {
    @Autowired
    DrugsMapper drugsMapper;
    @Autowired
    PurchaseMapper purchaseMapper;
    @Override
    public CommonResult addpurcahseimf(purchasegoods purchasegoods) {
        drugs drugs = new drugs();
        drugs.setAmount(purchasegoods.getAmount());
        drugs.setBatch_number(purchasegoods.getBatch_number());
        drugs.setDrug_id(purchasegoods.getDrug_id());
        drugs.setDrug_name(purchasegoods.getDrug_name());
        drugs.setDrug_type(purchasegoods.getDrug_type());
        drugs.setPackage_specification(purchasegoods.getPackage_specification());
        drugs.setProduct_date(purchasegoods.getProduct_date());
        drugs.setPrice(purchasegoods.getPrice());
        drugs.setQgp(purchasegoods.getQgp());
        drugs.setSupplier_id(purchasegoods.getSupplier_id());
        drugs.setPurchase_price(purchasegoods.getPurchase_price());
        int adddrugs = drugsMapper.Adddrugs(drugs);
        System.out.println(purchasegoods);
        if(adddrugs>0){
            int addpurcahseimf = purchaseMapper.addpurcahseimf(purchasegoods);
            if (addpurcahseimf>0){
                return new CommonResult(200,"进货成功");
            }else{
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new CommonResult(400, "进货失败");
            }
        }
        return  new CommonResult(400, "进货失败");
    }

    @Override
    public CommonResult GetOnepagesPurcahse(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<purchasegoods> getallpurchasegoods = purchaseMapper.Getallpurchasegoods();
        if (getallpurchasegoods.size() <= 0) {
            return new CommonResult(200, "暂时没有进货信息");
        }
        PageInfo pageInfo = new PageInfo(getallpurchasegoods);
        return new CommonResult(200, "进货信息第" + pageInfo.getPageNum() + "页获取成功", pageInfo);
    }

    @Override
    public CommonResult updatepurchasegoods(purchasegoods purchasegoods) {
        int i = purchaseMapper.updatepurchasegoods(purchasegoods);
        if(i>0){
            return new CommonResult(200, "修改成功", "药品id"+purchasegoods.getDrug_id());
        }
        return new CommonResult(401, "修改失败", "药品id"+purchasegoods.getDrug_id());
    }

    @Override
    public CommonResult deletepurchasegoods(Integer id, String drug_id) {
        int deletepurchasegoods = purchaseMapper.deletepurchasegoods(id, drug_id);
        if (deletepurchasegoods>0){
            return new CommonResult(200, "进货信息删除成功");

        }
        return new CommonResult(400, "进货信息删除失败，没有该信息");
    }

    @Override
    public CommonResult selectpurchase(String drug_id, String sale_time) {
        List<MInpurchase> selectpurchase = purchaseMapper.selectpurchase(drug_id, sale_time);
        return new CommonResult(200,"进货信息",selectpurchase);
    }
}
